package com.howie.parnote.jms.listener;

import com.howie.parnote.constant.ViewConstant;
import com.howie.parnote.jms.EmailMessageResolver;
import com.howie.parnote.module.EmailRecordModule;
import com.howie.parnote.module.LoginInfoModule;
import com.howie.parnote.util.FreemarkerRenderUtil;
import com.howie.parnote.util.jms.EmailUtil;
import freemarker.template.TemplateException;
import org.springframework.stereotype.Service;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>Title: parnote</p>
 * <p>
 * Description: parnote
 * </p>
 *
 * @author huangyan
 *         Created by HY on 2015/4/18 0018.
 * @version v1.0.0
 */
@Service
public class ConfirmEmailMessageListener extends EmailMessageResolver implements MessageListener {

	@Override
	public void onMessage(Message message) {
		if (logger.isDebugEnabled()) {
			logger.debug("进入邮件消息监听器");
		}
		if (message instanceof ObjectMessage) {
			try {
				String urlParam = resolveMessage(message);
				ObjectMessage objMessage = (ObjectMessage) message;
				LoginInfoModule loginInfoModule = (LoginInfoModule) objMessage.getObject();
				Map<String, String> parameters = new HashMap<>();
				parameters.put("urlParam", urlParam);
				String html = FreemarkerRenderUtil.getHtmlString(ViewConstant.VIEW_CONFIRM_MAIL, parameters);
				EmailUtil.sendHtmlMail(EmailUtil.FROM_EMAIL, loginInfoModule.getSharerEmail(), EmailUtil.USER_NAME, EmailUtil.PASSWORD, EmailUtil.CONFIRM_SUBJECT, html);
			} catch (JMSException e) {
				e.printStackTrace();
			} catch (TemplateException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	protected void fillEmailType(EmailRecordModule emailRecordModule) {
		emailRecordModule.setEmailType(EmailRecordModule.EmailType.CONFIRM_EMAIL.getValue());
	}
}
